package org.magento.pages;

import org.magento.data.CustomerData;
import org.magento.data.DefaultProduct;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

/*This class presents admin/sales_order_create page 
 * This is a main page for create new order from BE 
 * before get to this page user should select customer 
 */

public class CreateNewOrderBeckEnd extends Page {
	
	//Locators:
	@FindBy(id = "sales_order_create_customer_grid_filter_email")
	public WebElement textFieldEmail;
		
	@FindBy(xpath = "/descendant::td[@class='filter-actions a-right']/button[@title='Reset Filter']")
	public WebElement buttonResetCustomerFilter;
	
	@FindBy(xpath = "/descendant::td[@class='filter-actions a-right']/button[@title='Search']")
	public WebElement buttonCustomerSearch;
	
	@FindBy(xpath = "/descendant::table[@id='sales_order_create_customer_grid_table']/tbody")
	public WebElement searchResultLineCustomer; 
	
	@FindBy(xpath = "/descendant::div[@class='form-buttons']/button")
	public WebElement buttonAddProduct;
	
	@FindBy(xpath = "/descendant::td[@class='filter-actions a-right']/button[@title='Reset Filter']")
	public WebElement buttonResetProductFilter;
	
	@FindBy(id = "sales_order_create_search_grid_filter_name")
	public WebElement textFieldProductName;
	
	@FindBy(id = "sales_order_create_search_grid_filter_sku")
	public WebElement textFieldProductSku;
	
	//locator should be modified 
	@FindBy(xpath = "//div[@id='sales_order_create_search_grid']/table/tbody/tr/td[2]/button[2]")
	public WebElement buttonSearchProductFilter;	
	
	@FindBy(xpath ="/descendant::table[@id='sales_order_create_search_grid_table']/tbody")
	public WebElement searchResultProductFilter;
	
	//locator should be modified
	@FindBy (xpath = "//div[@id='order-search']/div/div/div/button")
	public WebElement buttonAddSelectedProduct;
	
	@FindBy (id = "order-billing_address_firstname")
	public WebElement textFieldFirstName;
	
	@FindBy (id = "order-billing_address_lastname")
	public WebElement textFieldLastName;
	
	@FindBy (id = "order-billing_address_street0")
	public WebElement textFieldAddress;
	
	@FindBy (id = "order-billing_address_city")
	public WebElement textFieldCity;
		
	@FindBy (id = "order-billing_address_country_id")
	public WebElement listCountry;
	
	@FindBy (id = "order-billing_address_postcode")
	public WebElement textFieldPostalCode;
	
	@FindBy (id = "order-billing_address_telephone")
	public WebElement textFieldTelephone;
	
	@FindBy (id = "p_method_checkmo")
	public WebElement radioButtonMoneyOrder;
	
	@FindBy (xpath = "/descendant::div[@id='order-shipping-method-summary']/a")
	public WebElement linkShipingMethod;
	
	@FindBy (id = "s_method_flatrate_flatrate")
	public WebElement radioButtonShippingFixedRate;
	
	@FindBy (xpath = "/descendant::div[@class='order-totals-bottom']/p/button")
	public WebElement buttonSubmitOrder;
	
	//Actions:
	
	
	//Select customer 
	public CreateNewOrderBeckEnd selectCustomer(CustomerData customer) throws Exception {
		buttonResetCustomerFilter.click();
		waitForThreeSeconds();
		type(textFieldEmail, customer.email);
		buttonCustomerSearch.click();
		waitForThreeSeconds();
		searchResultLineCustomer.click();
		waitForThreeSeconds();
		return PageFactory.initElements(driver, CreateNewOrderBeckEnd.class);
	}
	
	//Select a product
	public CreateNewOrderBeckEnd selectProduct(DefaultProduct product) throws Exception {
		buttonAddProduct.click();
		type(textFieldProductSku, product.sku);
		waitForFiveSeconds();
		buttonSearchProductFilter.click();
		waitForFiveSeconds();
		searchResultProductFilter.click();
		waitForThreeSeconds();
		buttonAddSelectedProduct.click();
		return PageFactory.initElements(driver, CreateNewOrderBeckEnd.class);
	}
	
	//Fill  Billing info 
	public CreateNewOrderBeckEnd fillInBillingData(CustomerData customer){
		type(textFieldFirstName, customer.firstName);
		type(textFieldLastName, customer.lastName);
		type(textFieldAddress, customer.address);
		type(textFieldCity, customer.city);
		select(listCountry, customer.country);
		type(textFieldPostalCode, customer.postcode);
		type(textFieldTelephone, customer.telephone);
		return PageFactory.initElements(driver, CreateNewOrderBeckEnd.class);
	}
	
	//Manage Payment Method select Check / Money order
	public CreateNewOrderBeckEnd managePaymentMethod() throws Exception { 
		waitForFiveSeconds();
		radioButtonMoneyOrder.click();		
		return PageFactory.initElements(driver, CreateNewOrderBeckEnd.class);
	}
	
	//ManageShipping method 
	public CreateNewOrderBeckEnd manageShippingMethod() throws Exception {
		waitForFiveSeconds();
		Actions action = new Actions(driver);
		action.moveToElement(linkShipingMethod).build().perform();
		linkShipingMethod.click();
		waitForFiveSeconds();
		radioButtonShippingFixedRate.click();
		return PageFactory.initElements(driver, CreateNewOrderBeckEnd.class);
	}
	
	//Submit order
	public OrderViewBeckEnd submitOrder() {
		buttonSubmitOrder.click();
		return PageFactory.initElements(driver, OrderViewBeckEnd.class);
	}

	
	//Assertions:
	//Auto-generated:
	public CreateNewOrderBeckEnd(WebDriver driver) {
		super(driver);
	}
	@Override
	public void open() {
	}
}
